/*
 * @Date: 2022-07-30 10:56:07
 * @LastEditors: Zzt
 * @LastEditTime: 2022-08-05 17:16:50
 * @FilePath: \archives-fe\src\view\appraisalAndDisposition\Destruction\contexts\DestructionContext.js
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import { APPROVAL, CHOICE, INCHOICE } from '../constants';
import { DestructionReducer } from '../reducers/DestructionReducer';
const initialState = {
  // sidebar的选择
  sidebar: '',
  // operateList
  operate: '',
  // 全部数据
  List: [
    { id: 1, name: '文书档案销毁处置', state: 2, opinions: 'wdad', dossier: '全宗1' },
    { id: 2, name: '文书档案销毁处置2', state: 1, opinions: '', dossier: '全宗2' },
    { id: 3, name: '文书档案销毁处置3', state: 0, opinions: '', dossier: '全宗3' },
    { id: 4, name: '文书档案销毁处置4', state: 0, opinions: '', dossier: '全宗2' }
  ],
  // 处理状态列表
  stateList: [
    { id: 0, name: '待审核', color: 'red' },
    { id: 1, name: '已通过', color: 'grey' },
    { id: 2, name: '审核返回', color: 'red' }
  ],
  // 鉴定审核列表
  appraisalList: [],
  // 查的列表
  operateList: []
};
const DestructionContext = createContext();
const DestructionContextProvider = (props) => {
  const [state, dispatch] = useReducer(DestructionReducer, initialState);
  const actions = {
    examineForm(data) {
      console.log(data);
      dispatch({ type: APPROVAL, data });
    },
    // 选择列表
    setSidebar(data) {
      dispatch({ type: CHOICE, data });
    },
    // 内部列表选择
    setOperate(data) {
      dispatch({ type: INCHOICE, data });
    }
  };
  const filters = {
    exportTypeFilter(id) {
      return state.exportTypeList.find((item) => item.id === id)?.name || '-';
    },
    stateFilter(id) {
      return state.stateList.find((item) => item.id === id)?.name || '-';
    },
    stateColorFilter(id) {
      return state.stateList.find((item) => item.id === id)?.color || 'red';
    }
  };

  useEffect(() => {}, []);
  return (
    <DestructionContext.Provider value={{ ...state, ...actions, ...filters }}>
      {props.children}
    </DestructionContext.Provider>
  );
};
export const useDestructionContext = () => {
  return useContext(DestructionContext);
};
export { DestructionContext, DestructionContextProvider };
